define(['../module'], function (module) {
  'use strict';
  module.controller('SaleOrderItemCtrl', function ($scope, $state, $stateParams, Common, Garment) {
    var id;
    id = $stateParams.id;
    $scope.mode = 'show';
    $scope.data = {
      order_lines: []
    };
    if (id) {
      Common('sale/order').get({
        id: $stateParams.id
      }, function (data) {
        $scope.data = data;
        $scope.edit = function () {
          return $state.go('.edit', $stateParams);
        };
        return $scope.viewLines = Garment.viewLine(data);
      });
    }
    Common('base/partner').query(function (partners) {
      return $scope.partners = partners;
    });
    Common('product/product').query(function (products) {
      return $scope.products = products;
    });


    $scope.new_view_line = function () {
      var newViewLine;
      newViewLine = Garment.newLine(null);
      return $scope.viewLines.push(newViewLine);
    };
    $scope["delete"] = function (index) {
      return $scope.viewLines.splice(index, 1);
    };
    $scope.input_qtys = function (viewLine) {
      if (!viewLine.product.id) {
        alert('请选择产品');
        return;
      }
      if (viewLine.colors.length < 1) {
        viewLine.setProduct(viewLine.product);
      }
      $scope.viewLine = viewLine;
      $('#qtyInputModal').modal({});
    };
    $scope.submit = function () {
      var i, len, order_lines, ref, viewLine;
      order_lines = [];
      ref = $scope.viewLines;
      for (i = 0, len = ref.length; i < len; i++) {
        viewLine = ref[i];
        order_lines = _.union(order_lines, viewLine.getOrderLineJson());
      }
      return $scope.message = order_lines;
    };

  });
})
